package com.deepinjava.dbbmlite

import java.util.regex.Matcher
import java.util.regex.Pattern

class DerbyProvider : BaseSqlProvider() {
    override val sysTimestamp = "CURRENT_TIMESTAMP"
    override val readPage = "OFFSET #offset ROWS FETCH NEXT #pagesize ROWS ONLY"
    override val readTop = "OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY"
    override val instrLoc = "locate(SCOL,'#1#1#1#1')"

    override val defINT = "int"
    override val defBIGINT = "bigint"
    override val defDOUBLE = "double"
    override val defDATETIME = "TIMESTAMP"

    override val updateSql: String
        get() {
            val pattern = """CONCAT ?\((.*),(.*?)\)"""
            return super.updateSql.replace(pattern.toRegex(), "$1||$2")
        }

    override val writeSql : String
        get() {
            val pattern = """CONCAT ?\((.*),(.*?)\)"""
            return  super.writeSql.replace(pattern.toRegex(), "$1||$2")
        }

    override fun getSumSql(i: Int): String {
        return super.getSumSql(i)
                .replace("SUBSTRING", "SUBSTR",ignoreCase = true)
    }

    override fun getFindSql(i: Int): String {
        val pattern = """CONCAT ?\((.*),(.*?)\)"""
        return  super.getFindSql(i).replace(pattern.toRegex(), "$1||$2")
    }

}
